package Eyjafjalla.action;

import Eyjafjalla.monster.DGDGZ;
import Eyjafjalla.monster.LTTQB;
import com.badlogic.gdx.Gdx;
import com.megacrit.cardcrawl.actions.AbstractGameAction;
import com.megacrit.cardcrawl.core.Settings;
import com.megacrit.cardcrawl.dungeons.AbstractDungeon;
import com.megacrit.cardcrawl.monsters.AbstractMonster;
import com.megacrit.cardcrawl.relics.AbstractRelic;

import java.util.Iterator;

public class SummonTQBAction extends AbstractGameAction {
    public SummonTQBAction(DGDGZ dgdgz){
        this.dgdgz = dgdgz;
        this.actionType = ActionType.SPECIAL;
        this.startDuration = 1.0F;
        this.duration = this.startDuration;
    }

    float targetDrawX;
    float targetDrawY;
    float xSpeed;
    float ySpeed;
    float yAccelerate;
    float rotationSpeed;
    float rotation;

    private int getSmartPosition(float targetDrawX) {
        int position = 0;

        for(Iterator var2 = AbstractDungeon.getCurrRoom().monsters.monsters.iterator(); var2.hasNext(); ++position) {
            AbstractMonster mo = (AbstractMonster)var2.next();
            if (!(targetDrawX > mo.drawX)) {
                break;
            }
        }

        return position;
    }

    @Override
    public void update() {
        if (startDuration == duration) {
            if (dgdgz == null || dgdgz.isDeadOrEscaped() || dgdgz.full()) {
                this.isDone = true;
                return;
            }
            else {
                for(int i =0;i<dgdgz.lttqbs.length;i++){
                    if(dgdgz.lttqbs[i]==null){
                        LTTQB tmp = new LTTQB(0,0);
                        dgdgz.lttqbs[i] = tmp;
                        this.lttqb = tmp;
                        lttqb.init();
                        lttqb.applyPowers();
                        lttqb.drawX = dgdgz.drawX;
                        lttqb.drawY = dgdgz.drawY+180F*Settings.yScale;
                        targetDrawX = dgdgz.drawX + DGDGZ.xPad[i]*Settings.xScale;
                        targetDrawY = dgdgz.drawY + DGDGZ.yPad[i]*Settings.yScale;
                        xSpeed = (targetDrawX-lttqb.drawX)/0.9F;
                        ySpeed = (lttqb.drawY-targetDrawY)*2F/0.9F;
                        yAccelerate = ySpeed/0.3F;
                        rotation = 180F;
                        rotationSpeed = 180F/0.9F;
                        //ROTATE 先试试顺时针旋转
                        lttqb.setRotation(rotation);

                        AbstractDungeon.getCurrRoom().monsters.addMonster(getSmartPosition(targetDrawX),lttqb);
                        Iterator var3 = AbstractDungeon.player.relics.iterator();
                        while(var3.hasNext()) {
                            AbstractRelic r = (AbstractRelic)var3.next();
                            r.onSpawnMonster(lttqb);
                        }
                        break;
                    }
                }
            }
        }
        else if(duration>=0.1F){
            float delta = Gdx.graphics.getDeltaTime();
            lttqb.drawX += delta*xSpeed;
            lttqb.drawY += delta*ySpeed;
            ySpeed -= delta*yAccelerate;
            rotation-=delta*rotationSpeed;
            lttqb.setRotation(rotation);
        }

        this.tickDuration();
        if(this.isDone){
            lttqb.drawX = targetDrawX;
            lttqb.drawY = targetDrawY;
            lttqb.setRotation(0F);
            lttqb.showHealthBar();
            lttqb.usePreBattleAction();
        }
    }

    DGDGZ dgdgz;
    LTTQB lttqb;
}
